/**
 * Copyright 2010 Pavel Daniel Alexandru PFA
 * 
 * Licensed under the terms of the GNU Lesser General Public License version 3 
 * as published by the Free Software Foundation. You may obtain a copy of the
 * License at: http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations 
 * under the License. 
 */
package com.kobol.webclient.client.signin.presenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.EventBus;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.Place;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.client.proxy.RevealRootContentEvent;
import com.kobol.webclient.client.NameTokens;
import com.kobol.webclient.client.signin.view.SignInPageUIHandlers;
import com.kobol.webclient.shared.FieldVerifier;

/**
 * @author Pavel Alexandru
 * 
 */
public class SignInPagePresenter extends Presenter<SignInPagePresenter.MyView, SignInPagePresenter.MyProxy> implements
	SignInPageUIHandlers {

    private final PlaceManager placeManager;
    private final ErrorDialogPresenterWidget errorDialog;

    @ProxyStandard
    @NameToken(NameTokens.signInPage)
    public interface MyProxy extends Proxy<SignInPagePresenter>, Place {
    }

    public interface MyView extends View, HasUiHandlers<SignInPageUIHandlers> {
	String getUserName();

	String getPassword();

	Button getSignInButton();

	void resetAndFocus();
    }

    @Inject
    public SignInPagePresenter(EventBus eventBus, MyView view, MyProxy proxy, PlaceManager placeManager,
	    ErrorDialogPresenterWidget errorDialog) {
	super(eventBus, view, proxy);

	this.placeManager = placeManager;
	this.errorDialog = errorDialog;

	getView().setUiHandlers(this);
    }

    @Override
    protected void onBind() {
	super.onBind();

	// register Handler's
	registerHandler(getView().getSignInButton().addClickHandler(new ClickHandler() {
	    @Override
	    public void onClick(ClickEvent event) {
		sendCredentialsToServer();
	    }
	}));
    }

    @Override
    protected void onReset() {
	super.onReset();
	getView().resetAndFocus();
    }

    @Override
    protected void revealInParent() {
	// RevealRootLayoutContentEvent.fire(this, this);
	RevealRootContentEvent.fire(this, this);
    }

    private void sendCredentialsToServer() {
	String userName = getView().getUserName();
	String password = getView().getPassword();

	if (FieldVerifier.isValidUserName(userName) && (FieldVerifier.isValidPassword(password))) {
	    PlaceRequest myRequest = new PlaceRequest(NameTokens.mainSmartGwtPage);

	    // If needed, add URL parameters in this way:
	    // myRequest = myRequest.with( "key1", "param1" ).with( "key2",
	    // "param2" );
	    placeManager.revealPlace(myRequest);
	} else {
	    showErrorDialog();
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.kobol.webclient.client.signin.view.SignInPageUIHandlers#showErrorDialog
     * ()
     */
    @Override
    public void showErrorDialog() {
	addToPopupSlot(errorDialog);
    }

}
